package com.timothy.android;

import java.util.ArrayList;
import java.util.List;

import com.timothy.chart.CanvasSetting;
import com.timothy.chart.Cylinder;
import com.timothy.question.BaseQuestion;
import com.timothy.question.TypeBean1;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;

public class CylinderAnswerActivity extends BaseActivity implements OnDoubleTapListener, OnGestureListener,OnTouchListener{
	private final static String LOG_TAG = "CylinderAnswerActivity";
	private TypeBean1 bean;
	List<Cylinder> cyliList;
	private int totalVotes = 0;

    private int verticalMinDistance = 20;
    private int minVelocity         = 0;
	GestureDetector mGestureDetector;
	
	BaseQuestion question = null;
	
	String questionName = null;
	String vote1 = null;
	String vote2 = null;
	String vote3 = null;
	String choice1 = null;
	String choice2 = null;
	String choice3 = null;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setContentView(new VerticalCylinderView(this));
		
		bean = TypeBean1.getMockInstance();
		
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		VerticalCylinderView view = new VerticalCylinderView(this);
		view.setOnTouchListener(this);
		view.setLongClickable(true);
		setContentView(view);
		
		//mock information
		Bundle bundle = this.getIntent().getExtras();
		questionName = bundle.getString("questionName");
		vote1 = bundle.getString("vote1");
		vote2 = bundle.getString("vote2");
		vote3 = bundle.getString("vote3");
		choice1 = bundle.getString("choice1");
		choice2 = bundle.getString("choice2");
		choice3 = bundle.getString("choice3");
		question = new BaseQuestion(1,questionName);
		
		//得到所有柱形的信息
		cyliList = new ArrayList<Cylinder>();
		Cylinder cylinder1 = new Cylinder(0,Integer.parseInt(vote1),choice1,1,Color.RED);
		cylinder1.setTopColor(Color.parseColor("#FF0000"));
		cylinder1.setBottomColor(Color.parseColor("#D40000"));
		totalVotes+=cylinder1.getVotes();
		cyliList.add(cylinder1);
		
		Cylinder cylinder2 = new Cylinder(1,Integer.parseInt(vote2),choice2,1,Color.GREEN);
		cylinder2.setTopColor(Color.GREEN);
		cylinder2.setBottomColor(Color.parseColor("#0C9720"));
		totalVotes+=cylinder2.getVotes();
		cyliList.add(cylinder2);
		
		if(choice3!=null&&!"".equals(choice3)){
			Cylinder cylinder3 = new Cylinder(1,Integer.parseInt(vote3),choice3,1,Color.BLUE);
			cylinder3.setTopColor(Color.BLUE);
			cylinder3.setBottomColor(Color.parseColor("#5056f4"));
			totalVotes+=cylinder3.getVotes();
			cyliList.add(cylinder3);			
		}
		
		mGestureDetector = new GestureDetector((OnGestureListener) this); 
//		setPreNextAct(Bar2DAnswerActivity.class,null);
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
//		Intent intent = new Intent(this, CallAndSMSInterceptor.class);
//		stopService(intent);
	}

	class VerticalCylinderView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;

		public VerticalCylinderView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();// 获得画布
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "VerticalCylinderView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "VerticalCylinderView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,1);
			
			canvas.drawColor(Color.BLACK);// 设置画布背景为黑色
			Paint mPaint = new Paint();
			mPaint.setColor(Color.WHITE);		
			
			mPaint.setTextSize(25);
			canvas.drawText(question.getQuestionName(), 50, 30, mPaint); 
			canvas.save();
			
			//根据图形个数既图形间距计算出图形宽度
			Log.i(LOG_TAG, "VerticalCylinderView:spaceInCharts:"+setting.getSpaceInCylis());
			int canvasNoMarginWidth = canvasWidth - setting.getLeftMargin() - setting.getRightMargin();
			double cylinderWidthInclSpace = (canvasNoMarginWidth * 1.0)/cyliList.size();
			int cylinderWidth = (int)(cylinderWidthInclSpace*0.9);
			setting.setSpaceInCylis((int)(cylinderWidthInclSpace-cylinderWidth));
			Log.i(LOG_TAG, "VerticalCylinderView:barWidth:"+cylinderWidth);
			
			for(int i=0;i<cyliList.size();i++){
				Cylinder cylinder = cyliList.get(i);
				
				int votes = cylinder.getVotes();
				double votesShare = (1.0* cylinder.getVotes())/totalVotes;
				int cylinderHeight = (int) (votesShare * canvasHeight);
				
				Log.i(LOG_TAG, "VerticalCylinderView:cylinderHeight:"+cylinderHeight);
				Log.i(LOG_TAG, "VerticalCylinderView:votes:"+votes);
				Log.i(LOG_TAG, "VerticalCylinderView:votesShare:"+votesShare);
				
				//produce answer content
				String answerText = null; 
				switch (cylinder.getAnswerType()){
				    case 0:answerText =  String.valueOf(votes);break; 
				    case 1:answerText =  String.valueOf(votesShare*100)+"%";break;
				    case 2:answerText =  String.valueOf(votes)+":"+String.valueOf(votesShare*100)+"%";break;
				}
				
				int tlX = (int) (setting.getLeftMargin()+ i * (cylinderWidth + setting.getSpaceInCylis()));
				int tlY =  canvasHeight - setting.getBottomMargin() - cylinderHeight;
				int brX = tlX + cylinderWidth;
				int brY = canvasHeight - setting.getBottomMargin();
				
				mPaint.setTextSize(25);
				
				canvas.drawText(answerText, tlX, tlY-20, mPaint);
				
				mPaint.setColor(cylinder.getTopColor());//圆柱顶部颜色
				canvas.drawOval(new RectF(tlX,tlY-20,brX,tlY+20), mPaint);
				
				mPaint.setColor(cylinder.getFullColor());//圆柱中间颜色
				canvas.drawRect(tlX,tlY,brX,brY, mPaint);
				
				mPaint.setColor(cylinder.getBottomColor());//圆柱底部颜色
				canvas.drawOval(new RectF(tlX,brY-20,brX,brY+20), mPaint);
				
				canvas.save();
			}

			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
		}
	}
	
	class HorizontalCylinderView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;

		public HorizontalCylinderView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();// 获得画布
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "HorizontalCylinderView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "HorizontalCylinderView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,1);
			
			canvas.drawColor(Color.BLACK);// 设置画布背景为黑色
			Paint mPaint = new Paint();
			mPaint.setColor(Color.WHITE);		
			
			mPaint.setTextSize(25);
			canvas.drawText(bean.getQuestion(), 50, 30, mPaint); 
			canvas.save();
			
			//根据图形个数既图形间距计算出图形宽度
			Log.i(LOG_TAG, "HorizontalCylinderView:spaceInCharts:"+setting.getSpaceInCylis());
//			int canvasNoMarginWidth = canvasWidth - setting.getLeftMargin() - setting.getRightMargin();
//			double cylinderWidthInclSpace = (canvasNoMarginWidth * 1.0)/cyliList.size();
//			int cylinderWidth = (int)(cylinderWidthInclSpace*0.9);
//			setting.setSpaceInCylis((int)(cylinderWidthInclSpace-cylinderWidth));
			
			int canvasNoMarginHeight = canvasHeight - setting.getLeftMargin() - setting.getRightMargin();
			double cylinderHeightInclSpace = (canvasNoMarginHeight * 1.0)/cyliList.size();
			int cylinderHeight = (int)(cylinderHeightInclSpace*0.9);
			setting.setSpaceInCylis((int)(cylinderHeightInclSpace-cylinderHeight));
			
			Log.i(LOG_TAG, "HorizontalCylinderView:barWidth:"+cylinderHeight);
			
			for(int i=0;i<cyliList.size();i++){
				Cylinder cylinder = cyliList.get(i);
				
				int votes = cylinder.getVotes();
				double votesShare = (1.0* cylinder.getVotes())/totalVotes;
//				int cylinderHeight = (int) (votesShare * canvasHeight);
				int cylinderWidth = (int) (votesShare * canvasWidth);
				
				Log.i(LOG_TAG, "HorizontalCylinderView:cylinderHeight:"+cylinderHeight);
				Log.i(LOG_TAG, "HorizontalCylinderView:votes:"+votes);
				Log.i(LOG_TAG, "HorizontalCylinderView:votesShare:"+votesShare);
				
				//produce answer content
				String answerText = null; 
				switch (cylinder.getAnswerType()){
				    case 0:answerText =  String.valueOf(votes);break; 
				    case 1:answerText =  String.valueOf(votesShare*100)+"%";break;
				    case 2:answerText =  String.valueOf(votes)+":"+String.valueOf(votesShare*100)+"%";break;
				}
				
				int tlX = setting.getLeftMargin();
				int tlY =  setting.getTopMargin()+ (int)(i*(cylinderHeight + setting.getSpaceInCylis()));
				int brX = tlX + cylinderWidth;
				int brY = tlY + cylinderHeight;
				
				mPaint.setTextSize(25);
				
				canvas.drawText(answerText, brX+20, tlY+cylinderHeight/2, mPaint);
				
				mPaint.setColor(cylinder.getTopColor());//圆柱顶部颜色
				canvas.drawOval(new RectF(tlX-20,tlY,tlX+20,brY), mPaint);
				
				mPaint.setColor(cylinder.getFullColor());//圆柱中间颜色
				canvas.drawRect(tlX,tlY,brX,brY, mPaint);
				
				mPaint.setColor(cylinder.getBottomColor());//圆柱底部颜色
				canvas.drawOval(new RectF(brX-20,tlY,brX+20,brY), mPaint);
				
				canvas.save();
			}

			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
		}
	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return mGestureDetector.onTouchEvent(event);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
        if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
            // 切换Activity
//        	 if(nextActivity!=null){
//                 Intent intent = new Intent(this, nextActivity);
//                 startActivity(intent);        		 
//        	 }
//            Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
        } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
//                Intent intent = new Intent(this, Bar2DAnswerActivity.class);
//                startActivity(intent); 
                Bundle bundle = new Bundle();
				Intent intent = new Intent();
			    intent.setClass(this, Bar2DAnswerActivity.class);
			    bundle.putString("questionName", questionName);
			    bundle.putString("vote1", vote1);
			    bundle.putString("vote2", vote2);
			    bundle.putString("vote3", vote3);
			    bundle.putString("choice1", choice1);
			    bundle.putString("choice2", choice2);
			    bundle.putString("choice3", choice3);
			    intent.putExtras(bundle);
			    startActivity(intent);	
        }
        return false;
	}
	
	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onDoubleTap(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onDoubleTapEvent(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onSingleTapConfirmed(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}


    
}
